package com.studysystem.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import com.studysystem.entity.User;
import com.studysystem.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import javax.servlet.http.HttpServletRequest;
import com.studysystem.repository.CourseRepository;
import com.studysystem.entity.Course;
import java.util.List;
import org.springframework.web.bind.annotation.PathVariable;
import com.studysystem.repository.CourseProgressRepository;
import com.studysystem.entity.CourseProgress;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;

@Controller
public class PageController {
    @Autowired
    private UserService userService;

    @Autowired
    private CourseRepository courseRepository;

    @Autowired
    private CourseProgressRepository courseProgressRepository;

    @GetMapping({"/", "/login"})
    public String login() {
        return "login";
    }

    @GetMapping("/register")
    public String register() {
        return "register";
    }

    @PostMapping("/register")
    public String doRegister(HttpServletRequest request, Model model) {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String email = request.getParameter("email");
        String fullName = request.getParameter("fullName");
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setEmail(email);
        user.setFullName(fullName);
        try {
            userService.registerUser(user);
        } catch (Exception e) {
            model.addAttribute("error", e.getMessage());
            return "register";
        }
        return "redirect:/login";
    }

    @GetMapping("/index")
    public String index() {
        return "index";
    }

    @GetMapping("/courses")
    public String courses(Model model) {
        List<Course> courseList = courseRepository.findAll();
        model.addAttribute("courses", courseList);
        return "courses";
    }

    @GetMapping("/progress")
    public String progress(Model model, @AuthenticationPrincipal UserDetails userDetails) {
        if (userDetails == null) {
            model.addAttribute("progressList", null);
            return "progress";
        }
        String username = userDetails.getUsername();
        Long userId = userService.findByUsername(username).map(u -> u.getId()).orElse(null);
        if (userId == null) {
            model.addAttribute("progressList", null);
            return "progress";
        }
        List<CourseProgress> progressList = courseProgressRepository.findByUserId(userId);
        model.addAttribute("progressList", progressList);
        return "progress";
    }

    @GetMapping("/test")
    public String test() {
        return "test";
    }

    @GetMapping("/discussion")
    public String discussion() {
        return "discussion";
    }

    @GetMapping("/review")
    public String review() {
        return "review";
    }

    @GetMapping("/courses/{id}")
    public String courseDetail(@PathVariable Long id, Model model) {
        Course course = courseRepository.findById(id).orElse(null);
        if (course == null) {
            return "redirect:/courses";
        }
        model.addAttribute("course", course);
        return "course_detail";
    }
} 